#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "QFileDialog"
#include <QMouseEvent>
#include <QtGui>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    setAcceptDrops(true);
    QString src = QFileDialog::getOpenFileName();
    QImage source(src);
    //QImage source(":/new/myibagens/fundoBom.jpg");
    QPixmap img = QPixmap::fromImage(source);
    ui->label_3->setPixmap(img);

}

void MainWindow::on_pushButton_2_clicked()
{
    QString src = QFileDialog::getOpenFileName();
    QImage source(src);
    QPixmap img = QPixmap::fromImage(source);
    ui->label_8->setPixmap(img);
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
 {
     if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
         if (event->source() == this) {
             event->setDropAction(Qt::MoveAction);
             event->accept();
         } else {
             event->acceptProposedAction();
         }
     } else {
         event->ignore();
     }
}

     void MainWindow::dragMoveEvent(QDragMoveEvent *event)
      {
          if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
              if (event->source() == this) {
                  event->setDropAction(Qt::MoveAction);
                  event->accept();
              } else {
                  event->acceptProposedAction();
              }
          } else {
              event->ignore();
          }
      }

     void MainWindow::dropEvent(QDropEvent *event)
      {
          if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
              QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
              QDataStream dataStream(&itemData, QIODevice::ReadOnly);

              QPixmap pixmap;
              QPoint offset;
              dataStream >> pixmap >> offset;

              QLabel *newIcon = new QLabel(this);
              newIcon->setPixmap(pixmap);
              newIcon->move(event->pos() - offset);
              newIcon->show();
              newIcon->setAttribute(Qt::WA_DeleteOnClose);

              if (event->source() == this) {
                  event->setDropAction(Qt::MoveAction);
                  event->accept();
              } else {
                  event->acceptProposedAction();
              }
          } else {
              event->ignore();
          }
      }

      void MainWindow::mousePressEvent(QMouseEvent *event)
      {
          QLabel *child = static_cast<QLabel*>(childAt(event->pos()));
          if (!child)
              return;

          QPixmap pixmap = *child->pixmap();

          QByteArray itemData;
          QDataStream dataStream(&itemData, QIODevice::WriteOnly);
          dataStream << pixmap << QPoint(event->pos() - child->pos());

          QMimeData *mimeData = new QMimeData;
          mimeData->setData("application/x-dnditemdata", itemData);

          QDrag *drag = new QDrag(this);
          drag->setMimeData(mimeData);
          drag->setPixmap(pixmap);
          drag->setHotSpot(event->pos() - child->pos());

          QPixmap tempPixmap = pixmap;
          QPainter painter;
          painter.begin(&tempPixmap);
          //painter.fillRect(pixmap.rect(), QColor(127, 127, 127, 127));
          painter.end();

          child->setPixmap(tempPixmap);

          if (drag->exec(Qt::CopyAction | Qt::MoveAction, Qt::CopyAction) == Qt::MoveAction)
              child->close();
          else {
              child->show();
              child->setPixmap(pixmap);
          }
      }
